package antlr4.definitive.d4_expr.d4_2_0_labeledExpr.run;

import antlr4.definitive.d4_expr.d4_2_0_labeledExpr.gen.LabeledExprLexer;
import antlr4.definitive.d4_expr.d4_2_0_labeledExpr.gen.LabeledExprParser;
import cn.hutool.core.io.FileUtil;
import org.antlr.v4.runtime.CharStreams;
import org.antlr.v4.runtime.CodePointCharStream;
import org.antlr.v4.runtime.CommonTokenStream;
import org.antlr.v4.runtime.tree.ParseTree;

import java.util.Scanner;

public class Calc {

    public static void main(String[] args) {
        CodePointCharStream codePointCharStream;
        if (args.length > 0) {
            String arg = args[0];
            String content = FileUtil.readString(arg, "UTF-8");
            System.out.println("读取文件" + arg + ",内容:\n" + content);
            codePointCharStream = CharStreams.fromString(content);
        } else {
            Scanner scanner = new Scanner(System.in);
            System.out.println("请输入一个 四则运算:");
            String input = scanner.nextLine();
            codePointCharStream = CharStreams.fromString(input);
        }
//
//        Scanner scanner = new Scanner(System.in);
//        System.out.println("请输入一个 四则运算:");
//
//        String input = scanner.nextLine();
//        CodePointCharStream codePointCharStream = CharStreams.fromString(input);
        //新建一个词法分析器，处理输入的CharStream
        LabeledExprLexer lexer=new LabeledExprLexer(codePointCharStream);
        CommonTokenStream tokens=new CommonTokenStream(lexer);
        LabeledExprParser parser=new LabeledExprParser(tokens);
        ParseTree tree = parser.prog();//开始语法分析
        EvalVisitor evalVisitor = new EvalVisitor();
        evalVisitor.visit(tree);

    }

}
